import {createSlice} from "@reduxjs/toolkit"
import {THConfirmModalProps} from "../../components/THModel"

export interface RootState {
  system: {
    show: boolean
    showToast: {
      show: boolean
      text: string
    }
    showConfirm: THConfirmModalProps
  }
}

export const systemSlice = createSlice({
  name: "system",
  initialState: {
    // 显示加载中
    show: false,
    // 显示信息窗口
    showToast: {
      show: false,
      text: "",
    },
    showConfirm: {
      isVisible: false,
      title: "提示",
      content: "",
      confirmText: "确认",
      cancelText: "取消",
    },
    // 显示确认框
  },
  reducers: {
    toggle: (state, action) => {
      state.show = action.payload
    },
    toggleToast: (state, action) => {
      // 是否显示，显示的文本 action.payload = {show,text}
      if (action.payload.show) {
        state.showToast.text = action.payload.text
      }
      state.showToast.show = action.payload.show
    },
    toggleConfirm: (state, action) => {
      state.showConfirm = action.payload
    },
  },
})

// 导出选择器
export const selectShow = (state: RootState) => state.system.show
export const selectShowToast = (state: RootState) => state.system.showToast
export const selectConfirm = (state: RootState) => state.system.showConfirm
export const {toggle, toggleToast, toggleConfirm} = systemSlice.actions
export default systemSlice.reducer
